import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/components/Login'
import Home from '@/components/home'
import Welcom from '@/components/Welcom'
import User from '@/components/user/users'
import Rights from '@/components/power/rights'
import Roles from '@/components/power/roles'
import Cate from '@/components/goods/cate'
import Params from '@/components/goods/params'

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', name: 'Login', component: Login },
    { path: '/home', name: 'Home', component: Home,redirect:'/welcom',
      children:[
        {path:'/welcom',component:Welcom},
        {path:'/users',component:User},
        {path:'/rights',component:Rights},
        {path:'/roles',component:Roles},
        {path:'/categories',component:Cate},
        {path:'/params',component:Params}
      ] 
    }
  ],
  mode: 'history'
})

//挂载路由守卫
router.beforeEach((to, form, next) => {
  //to是要访问的路径
  //from是代表从哪里来的路径
  //next是一个函数，表示放行 next('强制跳转的路径')
  if (to.path == '/login') return next();
  //只有登入获取token时才能登入成功
  //防止他人直接通过url跳页
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/login')
  next()
})
export default router